package com.doit.hbase.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;

import java.io.IOException;

/**
 * @DATE 2021/12/13/14:53
 * @Author MDK
 * @Version 2021.2.2
 **/
public class HbaseUtils {
    //获取连接对象
    public static Connection getConnection() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux01:2181,linux02:2181,linux03:2181");
        Connection conn = ConnectionFactory.createConnection(conf);
        return conn;
    }

    public static Admin getAdmin(Connection conn) throws Exception {
        return conn.getAdmin();
    }
    //获取表对象
    public static Table getTable(String tableName, Connection conn) throws Exception{
        Table table = conn.getTable(TableName.valueOf(tableName));
        return table;
    }

    //展示行数据
    public static void show(Result result){
        if (result != null){
            //迭代器遍历每行数据的单元格  判断是否有下一个单元格
            while(result.advance()){
                //获取当前的单元格
                Cell cell = result.current();
                //数据有自己的封装结构, 不好解析  要提供 Cell工具类
                byte[] cloneRow = CellUtil.cloneRow(cell);
                byte[] cloneFamily = CellUtil.cloneFamily(cell);
                byte[] cloneQualifier = CellUtil.cloneQualifier(cell);
                byte[] cloneValue = CellUtil.cloneValue(cell);

                System.out.println(new String(cloneRow)+":"+new String(cloneFamily)+":"+
                        new String(cloneQualifier)+":"+new String(cloneValue));

//                byte[] rowArray = cell.getRowArray();
//                System.out.println(new String(rowArray));
            }
        }
    }
}
